package com.sleep.satokendemo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sleep.satokendemo.entry.R;
import com.sleep.satokendemo.entry.XMLDemo;
import com.sleep.satokendemo.util.XMLUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author GuoAo
 * @className XMLDemoController
 * @date 2024/12/1
 * @description TODO
 */
@lombok.extern.slf4j.Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/xml")
public class XMLDemoController
{
    private final XMLUtil xmlUtil;
    @GetMapping(value = "/toXML",produces = "application/xml;charset=UTF-8")
    public R toXML(){
        return new R().setResult(new XMLDemo()
                .setUserName("Alice")
                .setPassword("123456")
                .setProject(Arrays.asList("Math","Chinese")))
                .setCode(200);
    }

    @GetMapping("/toJson")
    public R toJson(){
        return new R().setResult(new XMLDemo()
                .setUserName("Alice")
                .setPassword("123456")
                .setProject(Arrays.asList("Math","Chinese")))
                .setCode(200);
    }

    @GetMapping("/getXmlStr")
    public R getXmlStr() throws JsonProcessingException {
        return new R().setResult(xmlUtil.getXMLString(new XMLDemo()
                .setUserName("Alice")
                .setPassword("123456")
                .setProject(Arrays.asList("Math","Chinese"))))
                .setCode(200);
    }

    /**
     * @CachePut 注解 用于更新缓存 一般用于更新缓存的操作 @Cacheable 注解用于查询缓存 先查缓存查不到再执行方法查询
     * 需要在启动类上添加 @EnableCaching 注解
     * @param xmlDemo
     * @return
     */
    @CachePut(cacheNames = "xmlDemo",key = "#xmlDemo.userName",cacheManager = "cacheManager")
    @PostMapping("/sendXML")
    public R sendXML(@RequestBody XMLDemo xmlDemo){
        log.debug("xml:demo{}",xmlDemo);
        return new R().setResult(xmlDemo)
                .setCode(200);
    }
}
